import os
from core.database import PostgresManager

# in jupyter notebook current_path get with dir '.'
# current_path = os.path.abspath('.')
current_path = os.path.dirname(os.path.realpath(__file__))

if __name__ == "__main__":
    #print(current_path+"/../data/Iris.csv")
    try:
        pg_manager=PostgresManager(current_path+"/config.yaml")
        conn_str=pg_manager.get_connstr()
        print(conn_str)
        try:
            conn = pg_manager.connect()
            # 使用连接执行查询
            with conn.cursor() as cursor:
                cursor.execute("SELECT version()")
                print("PostgreSQL版本:", cursor.fetchone())
            print("\n连接成功！")
        except ConnectionError as e:
            print(f"\n连接失败: {e}")
        finally:
            pg_manager.close()
            print("\n连接已关闭")
    except ValueError as e:
        print(f"配置错误: {e}")
    except Exception as e:
        print(f"意外错误: {e}")